function []=ReadCVSplotSelective(startdir,variancefile,myxlabel,mname,plotautomatic)
%this is a function that plots a csv excel file that is comma delimited
% the sheet is assumed to have a set of headers and also an x axis measure
% an example of such a file is commented in the end
% use the gui to enter a plot

close all
clc

if(nargin==1)
    [FleName,PathName] = uigetfile('*.csv','chose the graph file',startdir);
elseif(nargin==0)
    [FleName,PathName] = uigetfile('*.csv','chose the graph file','.');
elseif(nargin>1)
    [PathName,FleName, ext] = fileparts(startdir) ;
    FleName=['/' FleName  ext];
end

if(size(PathName,2)==0)
    PathName='.';
end

filename=[PathName FleName ];

errorbarexists=0;



myylabel='mean squared error';

% skipping the header
M = csvread(filename,1,0);


%get the number of columns
n=size(M,2);



% read the header
fid=fopen(filename);
C_text=textscan(fid, '%s', n, 'delimiter', ',');
fclose(fid);

C_use=zeros(n,1);

for i=1:n
    C_text{1}(i)=strtrim(C_text{1}(i));
end


if(strcmp(C_text{1}(n),'0')~=0)
    n=n-1;
end

xValues=M(:,1);
dataPoints=M(:,2:n);
dataPointsbars=dataPoints;
algheight=20;
textboxheight=40;
bottonheight=20;
errorbarguiheight=20;
totalheight=(algheight*(n-1))+(textboxheight*3)+bottonheight+errorbarguiheight;

if(plotautomatic~=1)
    figure('MenuBar','none','Name',filename,'NumberTitle','off','Position',[360,500,400,totalheight]);
    
    uicells={};
    
    numusedAlgz=1;
    for i=2:n
        
        uicells{i-1}= uicontrol('Style','checkbox','String', C_text{1}(i),'Value',0,'Position',[ 0 totalheight-(numusedAlgz*algheight) 400 20]);
        numusedAlgz=numusedAlgz+1;
    end
    
    
    
    Txt = uicontrol('Style','Edit','Position',[0,totalheight-((algheight*(n-1)))-textboxheight,400,textboxheight]);
    
    [~,graphtitle,~] = fileparts(filename);
    set( Txt, 'String',graphtitle);
    
    Txtlabel = uicontrol('Style','Edit','Position',[0,totalheight-((algheight*(n-1)))-(2*textboxheight),400,textboxheight]);
    set( Txtlabel, 'String', C_text{1}(1));
    
    
    uicontrol('Style','PushButton','String','Load Error Bars Variance File','Position',[ 0 ,totalheight-((algheight*(n-1)))-(2*textboxheight)-errorbarguiheight, 400, errorbarguiheight],'CallBack',@okerrorbarpressed);
    
    
    uicontrol('Style','PushButton','String','OK','Position',[ 0 ,totalheight-((algheight*(n-1)))-(2*textboxheight)-errorbarguiheight-bottonheight, 400, bottonheight],'CallBack',@okpressed);
    
    Txtlabelfbar = uicontrol('Style','Edit','Position',[0,totalheight-((algheight*(n-1)))-(3*textboxheight)-errorbarguiheight-bottonheight,400,textboxheight]);
    set( Txtlabelfbar, 'String', 'variance file not loaded');
    

    
else
    
    for i=2:n
        C_use(i-1,1) =1;;
    end
    plotgraph(C_use,xValues,dataPoints,dataPointsbars,myxlabel,myylabel, C_text,PathName,errorbarexists,mname );
    
end

    function okerrorbarpressed(h, eventdata)
    
    if(nargin==1)
        [FleNameeb,PathNameeb] = uigetfile('*.csv','chose the variance file',startdir);
    elseif(nargin==0)
        [FleNameeb,PathNameeb] = uigetfile('*.csv','chose the graph file','.');
    elseif(nargin==2)
        [FleNameeb,PathNameeb, ext] = fileparts(startdir) ;
        FleNameeb=['/' FleName  ext];
    end
    
    
    
    filenameeb=[PathNameeb FleNameeb ];
    
    if(exist(filenameeb,'file')~=0)
        errorbarexists=1;
        set( Txtlabelfbar,'String', filenameeb);
        MB = csvread(filenameeb,1,0);
        dataPointsbars=MB(:,2:n);
    else
        display(['file '  ' does not exist, no error bars will be used']);
        
    end
    
    
    
    end
    
    function okpressed(h, eventdata)
    myylabel=get( Txt, 'String');
    myxlabel=get( Txtlabel, 'String');
    for i=2:n
        C_use(i-1,1) = get(uicells{i-1}, 'Value');
    end
    
    folder_name = uigetdir('.');
    [pathstr, name, ext] = fileparts(filename);
    sep = filesep;
    newfname=[ folder_name  sep  name];
    plotgraph(C_use,xValues,dataPoints,dataPointsbars,myxlabel,myylabel, C_text,newfname,errorbarexists ,mname);
    
    end

end

function []=plotgraph(C_use,xValues,dataPoints, dataPointsbars,myxlabel,myylabel, C_text,newfname,errorbarexists ,mname)





delete(gcf);
linestyles = cellstr(char('-',':','-.','--','-',':','-.','--','-',':','-',':','-.','--','-',':','-.','--','-',':','-.'));
%  MarkerEdgeColors=jet(2*size(find(C_use),1)); % n is the number of different items you have
MarkerEdgeColors =  [1.0000         0         0
    0    0         0
    0    1.0000    0.4000
    0    0.4000    1.0000
    0.8000         0    1.0000
    0.2         1     0.6
    0    0.5       1
    1     1    0.4
    ];

Markers=['o','x','+','*','s','d','v','^','<','>','p','h','.','+','*','o','x','^','<','h','.','>','p','s','d','v','o','x','+','*','s','d','v','^','<','>','p','h','.'];

indicex=find(C_use);
figure
hold on

for i=1:size(indicex,1)
    if( errorbarexists==0)
        p=plot(xValues,dataPoints(:,indicex(i) ),[linestyles{i} Markers(i)],'Color',MarkerEdgeColors(i,:));
    else
        p=errorbar(xValues,dataPoints(:,indicex(i) ),sqrt(dataPointsbars(:,indicex(i) )),[linestyles{i} Markers(i)],'Color',MarkerEdgeColors(i,:));
    end
    set(p,'LineWidth',2);
end



ylim([0 max(max(dataPoints(:,indicex' )))+(0.1*max(max(dataPoints)))]);
xlabel(myxlabel,'FontSize',14);
ylabel(myylabel,'FontSize',14);
% title(graphtitle,'FontSize',14);
legend( C_text{1}(find(C_use)+1),'Location','NorthWest','FontSize',14);


saveas(p,[newfname '/graph' mname '.fig']);
saveas(p,[newfname '/graph' mname '.jpg']);
saveas(p,[newfname '/graph' mname '.png']);
saveas(p,[newfname '/graph' mname '.eps'],'epsc');
print(gcf, '-dpdf', '-r600', [newfname '/' mname 'graph.pdf'])

end



%
%
%
%
%
% errorratio  	 ,  COOK Robust Std  	 ,  COOK Robust Tdd Test  	 ,  Cook with SPRT and SPRT OR COOK Stop  	 ,  GUIDED_MLESAC_F  	 ,  MSAC with Tdd Test  	 ,  MSAC_F  	 ,  RANSAC_F  	 ,  SPRT 1  	 ,
%  0  	 ,0.06132 	 , 0.061495 	 , 0.04641 	 , 0.009875 	 , 0.002445 	 , 0.003765 	 , 0.00361 	 , 0.001985 	 ,
%  0.05  	 ,0.063285 	 , 0.0648 	 , 0.046115 	 , 0.014055 	 , 0.003645 	 , 0.005585 	 , 0.005665 	 , 0.001485 	 ,
%  0.1  	 ,0.08088 	 , 0.079315 	 , 0.056925 	 , 0.02156 	 , 0.0062 	 , 0.00826000000000001 	 , 0.007915 	 , 0.003255 	 ,
%  0.15  	 ,0.07634 	 , 0.0779 	 , 0.053895 	 , 0.03008 	 , 0.008635 	 , 0.011135 	 , 0.01136 	 , 0.00344 	 ,
%  0.2  	 ,0.07 	 , 0.07325 	 , 0.049095 	 , 0.040965 	 , 0.011 	 , 0.015595 	 , 0.015185 	 , 0.00383 	 ,
%  0.25  	 ,0.0733 	 , 0.07791 	 , 0.049205 	 , 0.063465 	 , 0.013995 	 , 0.023485 	 , 0.022305 	 , 0.006255 	 ,
%  0.3  	 ,0.0804299999999999 	 , 0.080025 	 , 0.04974 	 , 0.089635 	 , 0.020055 	 , 0.03318 	 , 0.032915 	 , 0.007285 	 ,
%  0.35  	 ,0.08318 	 , 0.088795 	 , 0.051395 	 , 0.138835 	 , 0.029455 	 , 0.04932 	 , 0.04867 	 , 0.009985 	 ,
%  0.4  	 ,0.094625 	 , 0.098855 	 , 0.05216 	 , 0.219435 	 , 0.048845 	 , 0.07974 	 , 0.07773 	 , 0.014575 	 ,
%  0.45  	 ,0.10788 	 , 0.10356 	 , 0.056555 	 , 0.36693 	 , 0.08122 	 , 0.130925 	 , 0.128495 	 , 0.023175 	 ,
%  0.5  	 ,0.12285 	 , 0.10776 	 , 0.05858 	 , 0.57024 	 , 0.12421 	 , 0.20314 	 , 0.199125 	 , 0.04023 	 ,
%  0.55  	 ,0.153385 	 , 0.12608 	 , 0.064165 	 , 0.89341 	 , 0.208045 	 , 0.321735 	 , 0.31173 	 , 0.071175 	 ,
%  0.6  	 ,0.20124 	 , 0.166975 	 , 0.076785 	 , 1.51428 	 , 0.326575 	 , 0.54203 	 , 0.522290000000001 	 , 0.158295 	 ,
%  0.65  	 ,0.36805 	 , 0.275915 	 , 0.13912 	 , 2.25161 	 , 0.496635 	 , 0.807065 	 , 0.772815 	 , 0.33174 	 ,
%
%
